package com.weihao;

import com.weihao.pojo.User;
import com.weihao.util.MyBatisUtil;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.IOException;
import java.util.List;

public class test {
    public static void main(String[] args) throws IOException {
        //1、创建SqlSessionFactory对象，也是单例模式的
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        //2、创建SqlSession对象
        SqlSession session = factory.openSession();
//        //3、调用session的方法namespace的名字.id
//        List<User> users = session.selectList("user.list");
//        //4、打印测试
//        for(User u:users){
//            System.out.println(u);
//        }
//        //5、关闭资源
//        User user = session.selectOne("user.getUserById", 1);
//        System.out.println(user);
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.list();
        for(User u:users){
            System.out.println(u);
        }
        //根据id删除指定数据
        mapper.deleteUserById(1);
        System.out.println("删除后数据");
        users = mapper.list();
        for(User u:users){
            System.out.println(u);
        }
        //添加数据
        mapper.addUser(new User(1, "weihao", 18));
        System.out.println("添加后数据");
        users = mapper.list();
        for(User u:users){
            System.out.println(u);
        }
        //根据id修改age
        mapper.updateUserById(new User(1, "yo", 20));
        System.out.println("修改后数据");
        users = mapper.list();
        for(User u:users){
            System.out.println(u);
        }
        session.commit();
        session.close();
    }
}
